package com.nondo.dean.platform.service;

import com.nondo.dean.common.domain.BaseEntity;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.SystemArticle;
import com.nondo.dean.platform.domain.SystemArticleExample;
import com.nondo.dean.platform.mapper.SystemArticleMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/25
 */
@Component
public class SystemArticleService {
    @Autowired
    SystemArticleMapper systemArticleMapper;

    /**
     * 添加
     *
     * @param systemArticle
     * @return
     */
    public int add(SystemArticle systemArticle) {
        return this.systemArticleMapper.insertSelective(systemArticle);
    }

    /**
     * 更新
     *
     * @param systemArticle
     * @return
     */
    @Transactional
    public int update(SystemArticle systemArticle) {
        return this.systemArticleMapper.updateByPrimaryKeySelective(systemArticle);
    }

    /**
     * 删除
     *
     * @param systemArticle
     * @return
     */
    public int delete(SystemArticle systemArticle) {
        systemArticle.setDelFlag(SystemArticle.DEL_FLAG_DELETE);
        return this.systemArticleMapper.updateByPrimaryKeySelective(systemArticle);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public SystemArticle select(Integer id) {
        return this.systemArticleMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据类型查询
     *
     * @param type
     * @return
     */
    public SystemArticle selectByType(String type) {
        SystemArticleExample articleExample = new SystemArticleExample();
        articleExample.createCriteria().andTypeEqualTo(type).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);

        List<SystemArticle> articleList = this.systemArticleMapper.selectByExampleWithBLOBs(articleExample);

        return CollectionUtils.isNotEmpty(articleList) ? articleList.get(0) : null;
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param systemArticle
     * @return
     */
    public List<SystemArticle> getList(int offset, int limit, SystemArticle systemArticle) {
        SystemArticleExample example = new SystemArticleExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        SystemArticleExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, systemArticle);
        List<SystemArticle> articleList = systemArticleMapper.selectByExampleWithRowbounds(example, rowBounds);

        return articleList;
    }

    /**
     * 数据统计
     *
     * @param systemArticle
     * @return
     */
    public Long count(SystemArticle systemArticle) {
        SystemArticleExample example = new SystemArticleExample();
        SystemArticleExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, systemArticle);
        return systemArticleMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param systemArticle
     */
    void setCriteria(SystemArticleExample.Criteria criteria, SystemArticle systemArticle) {
        if (systemArticle != null) {
            if (systemArticle.getId() != null) {
                criteria.andIdEqualTo(systemArticle.getId());
            }
            if (StringUtils.isNoneBlank(systemArticle.getTitle())) {
                criteria.andTitleEqualTo(systemArticle.getTitle());
            }
        }
        criteria.andDelFlagEqualTo(SystemArticle.DEL_FLAG_NORMAL);
    }
}
